<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//敏感词管理
class Sensitive extends Controller
{
    //
    public function sw_index(Request $request)
    {
        //实现分页
        $data['page'] = (int)$request->page;//获取分页
        $data['pageSize'] = 10;
        $pages = DB::table('article_swd')->orderBy('id','desc')->pages($data['pageSize']);
        $data['cate_sw'] = $pages['lists'];
        $data['total']        = $pages['total']; //总数
        return view('admin.setting.sw_index',$data);
    }
    public function sw_add(Request $request)
    {
        $mid = (int)$request->mid;
        $data['sw'] = DB::table('article_swd')->where('id',$mid)->item();

        return view('admin.setting.sw_add',$data);
    }
    public function sw_save(Request $request)
    {
        $mid = (int)$request->mid;
        $name = trim($request->name);
        if ($name == ''){
            exit(json_encode(array('code'=>1,'msg'=>'敏感词不能为空')));
        }
        $res = DB::table('article_swd')->where('name',$name)->first();
        if ($mid == 0){
            if ($res){
                exit(json_encode(array('code'=>1,'msg'=>'敏感词已经存在')));
            }
            DB::table('article_swd')->insert(array('name'=>$name));
        }else{
            DB::table('article_swd')->where('id',$mid)->update(array('name'=>$name));
        }
        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }
    public function del_sw(Request $request)
    {
        $mid = (int)$request->mid;
        DB::table('article_swd')->where('id',$mid)->delete();
        exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
    }
}
